package org.graduation.project.controller;

import org.bouncycastle.cert.ocsp.Req;
import org.graduation.common.NoPermissionException;
import org.graduation.feignapi.RBACClient;
import org.graduation.model.common.RBAC.Permissions;
import org.graduation.model.common.ResponseResult;
import org.graduation.model.common.project.Task;
import org.graduation.model.common.user.User;
import org.graduation.model.dtos.RBAC.CheckPermissionsDTO;
import org.graduation.model.dtos.project.CreateTaskDTO;
import org.graduation.project.mapper.MybatisPlusWrapperFactory;
import org.graduation.project.service.TaskAndUserService;
import org.graduation.project.service.TaskService;
import org.graduation.utils.ResponseResultUtil;
import org.graduation.utils.Token;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@RestController
@RequestMapping("/project/phase/task")
public class TaskController {
    @Autowired
    private RBACClient rbacClient;
    @Autowired
    private Token token;
    @Autowired
    private TaskService taskService;
    @Autowired
    private TaskAndUserService taskAndUserService;

    @RequestMapping(value = "/create", method = RequestMethod.POST)
    public ResponseResult create(@RequestBody CreateTaskDTO dto, HttpServletRequest request) {
        Task task = dto.getTask();
        int uid = token.getUidByToken(token.getTokenByRequest(request));
        Permissions permission = Permissions.CREATE_TASK;
        if (!checkPermission(task.getPId(), uid, permission)) {
            throw new NoPermissionException(permission);
        }
        task.setCreateUser(uid);
        task.initStatus();
        taskService.save(task);
        taskAndUserService.saveBatch(dto.getTaskAndUserTables(task.getTaskId()));
        return ResponseResult.success(task);
    }

    @RequestMapping(value = "/view", method = RequestMethod.GET)
    public ResponseResult view(@RequestParam Long pId, @RequestParam Long phaseId, HttpServletRequest request) {
        int uid = token.getUidByToken(token.getTokenByRequest(request));
        if (!checkPermission(pId, uid, Permissions.VIEW_TASK)) {
            throw new NoPermissionException(Permissions.VIEW_TASK);
        }
        List<Task> taskList = taskService.list(MybatisPlusWrapperFactory.getTaskByPhaseId(phaseId));
        return ResponseResult.success(taskList);
    }

    @RequestMapping(value = "/info", method = RequestMethod.GET)
    public ResponseResult info(@RequestParam Long taskId, HttpServletRequest request) {
        Permissions permission = Permissions.VIEW_TASK;
        int uid = token.getUidByToken(token.getTokenByRequest(request));
        Task task = taskService.getById(taskId);
        if (!checkPermission(task.getPId(), uid, permission)) {
            throw new NoPermissionException(permission);
        }
        return ResponseResult.success(task);
    }

    @RequestMapping(value = "/changeStatus", method = RequestMethod.POST)
    public ResponseResult changeStatus(@RequestBody Task task, HttpServletRequest request) {
        Permissions permission = Permissions.CHANGE_TASK_STATUS;
        int uid = token.getUidByToken(token.getTokenByRequest(request));
        if (!checkPermission(task.getPId(), uid, permission)) {
            throw new NoPermissionException(permission);
        }
        taskService.updateById(task);
        return ResponseResult.success();
    }

    @RequestMapping(value = "/real_delete", method = RequestMethod.DELETE)
    public ResponseResult realDelete(@RequestBody Task task, HttpServletRequest request){
        Permissions permission = Permissions.DELETE_TASK;
        int uid = token.getUidByToken(token.getTokenByRequest(request));
        if (!checkPermission(task.getPId(), uid, permission)) {
            throw new NoPermissionException(permission);
        }
        return ResponseResultUtil.simpleCheck(taskService.realDeleteTask(task));
    }

    @RequestMapping(value = "/alarmTask", method = RequestMethod.GET)
    public List<Task> alarmTask(@RequestParam Integer uid){
        User u = new User();
        u.setUid(uid);
        return taskService.getAllAlarmTask(u);
    }

    private boolean checkPermission(Long pId, Integer uid, Permissions permission) {
        return rbacClient.check(new CheckPermissionsDTO(pId, uid, permission)).getCode() == 200;
    }
}
